package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcLabOrderEntity;
import com.dhcc.cdc.busi.service.ICdcLabOrderService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 检验医嘱
 * 
 * @ClassName: LabOrderController
 * @Description: TODO
 * @author jumper
 * @date 2021-04-26 16:22:26
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdclaborder")
public class CdcLabOrderController extends BaseController {

	private String PREFIX = "/backend/cdc/laborder/";

	@Autowired
	private ICdcLabOrderService labOrderService;
	

    /**
     * 跳转到检验医嘱列表首页
     */
    @RequestMapping("")
    @Permission("laborder:list")
    public String index() {
        return PREFIX + "laborder.html";
    }
	
    /**
     * 跳转到新增检验医嘱
     */
    @RequestMapping("/to_add")
    @Permission("laborder:add")
    public String toAdd() {
        return PREFIX + "laborder_add.html";
    }
	
	/**
     * 跳转到修改检验医嘱
     */
    @RequestMapping("/to_update/{labOrderId}")
    @Permission("laborder:update")
    public String toUpdate(@PathVariable Long labOrderId, Model model) {
        CdcLabOrderEntity labOrder = this.labOrderService.selectById(labOrderId);
        model.addAttribute("labOrder",labOrder);
        return PREFIX + "laborder_edit.html";
    }

    /**
     * 获取检验医嘱列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("laborder:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcLabOrderEntity> page = new PageFactory<CdcLabOrderEntity>().defaultPage();
        List<CdcLabOrderEntity> result = this.labOrderService.page(page,params);
        page.setRecords((List<CdcLabOrderEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增检验医嘱
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("laborder:add")
    @BussinessLog(value = "新增检验医嘱")
    public Object add(CdcLabOrderEntity labOrder) {
        if (CommonUtil.isOneEmpty(labOrder)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        labOrder.insert();
        return R.ok();
    }

    /**
     * 删除检验医嘱
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("laborder:delete")
    @BussinessLog(value = "删除检验医嘱")
    public Object delete(@RequestParam Long labOrderId) {
	
        this.labOrderService.deleteById(labOrderId);

        return R.ok();
    }

    /**
     * 修改检验医嘱
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("laborder:update")
    @BussinessLog(value = "修改检验医嘱")
    public Object update(CdcLabOrderEntity labOrder) {
        if (CommonUtil.isOneEmpty(labOrder)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcLabOrderEntity old = this.labOrderService.selectById(labOrder.getId());

        old.setXcode(labOrder.getXcode());
        old.setCode(labOrder.getCode());
        old.setDescription(labOrder.getDescription());
        old.setNote(labOrder.getNote());
        old.setBusLabCateId(labOrder.getBusLabCateId());
        old.setDataId(labOrder.getDataId());
        old.setIsActive(labOrder.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
